enum dataBlock {
    //% block="1"
1,
    //% block="2"
     2,
    //% block="3"
3,
    //% block="4"
 4,
    //% block="5"
5,
    //% block="6"
6,
    //% block="7"
7,
    //% block="8"
8,
    //% block="9"
9,
    //% block="10"
10,
    //% block="11"
11,
    //% block="12"
12,
    //% block="13"
13,
    //% block="14"
14,
    //% block="15"
    15,
    //% block="16"
16,
    //% block="17"
17,
    //% block="18"
 18,
    //% block="19"
19,
    //% block="20"
20,
    //% block="21"
21,
    //% block="22"
22,
    //% block="23"
23,
    //% block="24"
 24,
    //% block="25"
 25,
    //% block="26"
26,
    //% block="27"
 27,
    //% block="28"
28,
    //% block="29"
29,
    //% block="30"
30,
    //% block="31"
31,
    //% block="32"
32,
    //% block="33"
 33,
    //% block="34"
 34,
    //% block="35"
35,
    //% block="36"
36,
    //% block="37"
37,
    //% block="38"
38,
    //% block="39"
39,
    //% block="40"
40,
    //% block="41"
41,
    //% block="42"
 42,
    //% block="43"
 43,
    //% block="44"
 44,
    //% block="45"
 45,
    //% block="46"
 46,
    //% block="47"
47,
    //% block="48"
 48,
    //% block="49"
49,
    //% block="50"
50,
    //% block="51"
51,
    //% block="52"
 52,
    //% block="53"
53,
    //% block="54"
 54,
    //% block="55"
55,
    //% block="56"
 56,
    //% block="57"
 57,
    //% block="58"
  58,
    //% block="59"
59,
    //% block="60"
 60,
    //% block="61"
61,
    //% block="62"
62,
}
enum ByteID {
    //% block="1"
1,
    //% block="2"
     2,
    //% block="3"
3,
    //% block="4"
 4,
    //% block="5"
5,
    //% block="6"
6,
    //% block="7"
7,
    //% block="8"
8,
    //% block="9"
9,
    //% block="10"
10,
    //% block="11"
11,
    //% block="12"
12,
    //% block="13"
13,
    //% block="14"
14,
    //% block="15"
    15,
    //% block="16"
16
}



//% color="#5ba55b" iconWidth=50 iconHeight=40
namespace MFRC522_I2C {
  
    //% block="初始化RFID模块 接口I2C 地址0x28" blockType="command"
    //% TXPIN.shadow="dropdown" TXPIN.options="TXPIN"
    export function RFID_Setup(parameter: any, block: any) {
       // let TXPIN = parameter.TXPIN.code
        Generator.addInclude('ruilong_Wire', '#include <Wire.h>');
        Generator.addInclude('ruilong_MFRC522_I2C', '#include <RL_MFRC522_I2C.h>');
        Generator.addObject("object_MFRC522", `MFRC522 `, ` rfid(0x28,-1);`);
        Generator.addSetup(`initWire_begin`, `Wire.begin();`);
        Generator.addSetup(`initrfid`, `rfid.PCD_Init();`);
    }

    //% block="RFID检测到卡片？" blockType="boolean"
    export function RFID_available() {
        Generator.addCode(`rfid.IsNewCard()`)
    }
    //% block="读取RFID模块UID" blockType="reporter"
    export function RFIDdata(parameter: any, block: any) {
    Generator.addCode(`rfid.ReadCardUID()`);
    }
    
    //% block="读取RFID模块 数据块[dataBlock]第[ByteID]字节" blockType="reporter"
    //% dataBlock.shadow="dropdown" dataBlock.options="dataBlock" dataBlock.defl="1"
    //% ByteID.shadow="dropdown" ByteID.options="ByteID" ByteID.defl="1"
    export function RFIDReadCardbyte(parameter: any, block: any) {
        let dataBlock = parameter.dataBlock.code;
        let ByteID = parameter.ByteID.code;
    Generator.addCode(`rfid.ReadCardbyte( ${dataBlock}, ${ByteID})`);
    }


    //% block="RFID模块 数据块[dataBlock]第[ByteID]字节写入[data]" blockType="command"
    //% dataBlock.shadow="dropdown" dataBlock.options="dataBlock" dataBlock.defl="1"
    //% ByteID.shadow="dropdown" ByteID.options="ByteID" ByteID.defl="1"
    //% data.shadow="range"   data.params.min=0    data.params.max=255    data.defl=100  
    export function RFIDWriteCardbyte(parameter: any, block: any) {
        let dataBlock = parameter.dataBlock.code;
        let ByteID = parameter.ByteID.code;
        let data = parameter.data.code;
        Generator.addCode(`rfid.WriteCardbyte( ${dataBlock}, ${ByteID},${data});`);
    }

    //% block="RFID模块 终止卡片通讯" blockType="command"
      export function RFIDStopComm(parameter: any, block: any) {
        Generator.addCode(`rfid.PICC_HaltA();\nrfid.PCD_StopCrypto1();`);
    }
}


